﻿Imports Runa.Scripting.ScriptingHost
Imports Runa.Resources.ResourceManager
Imports Runa.Common.Helper
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Input
Imports Runa.UI.Effects
Imports Runa.Foundation.Diagnostics
Imports System.Reflection
Imports Runa.Foundation.Runtime
Imports Microsoft.Scripting.Hosting

Partial Public Class GameRender

    Public Property ScriptObjects() As New Dictionary(Of String, ScriptTask)

    Private Sub AddBasicReference(ByVal spt As ScriptTask)
        With spt
            .LoadAssembly(Me.GetType().Assembly)
            .LoadAssembly(GetType(SpriteFont).Assembly)
            .LoadAssembly(GetType(SpriteBatch).Assembly)
            .LoadAssembly(GetType(GraphicsDevice).Assembly)
            .LoadAssembly(GetType(Vector2).Assembly)
            .LoadAssembly(GetType(Texture2D).Assembly)
            .LoadAssembly(GetType(GameTime).Assembly)
            .LoadAssembly(GetType(GameTimeEventArgs).Assembly)

            .LoadAssembly(GetType(GamePad).Assembly)

            .LoadAssembly(GetType(FontObject).Assembly)
            .LoadAssembly(GetType(Texture2DObject).Assembly)
            .LoadAssembly(GetType(AudioObject).Assembly)

            .LoadAssembly(GetType(Runa.UI.Effects.WpfEffectBase).Assembly)

#If Not NETFX_CORE Then
            .LoadAssembly(GetType(MessageBox).Assembly)
#End If
        End With
    End Sub

    ''' <summary>
    ''' Binding the OnRuntimeError event of all ScriptTask objects to one event routine.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub BindingScriptTaskOnErrorEvent()
        For Each item In Me.ScriptObjects
            AddHandler item.Value.OnRuntimeError, AddressOf ScriptTask_OnRuntimeError
        Next
    End Sub

    ''' <summary>
    ''' Binding the OnCompilationError event of all ScriptTask objects to one event routine.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub BindingScriptTaskOnOnCompilationFailedEvent()
        For Each item In Me.ScriptObjects
            AddHandler item.Value.OnCompilationError, AddressOf ScriptTask_OnCompilationFailed
        Next
    End Sub

    ''' <summary>
    ''' Handles OnRuntimeError events of all the ScriptTask objects.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ScriptTask_OnRuntimeError(ByVal sender As Object, ByVal e As RuntimeErrorEventArgs)
        Dim msg = String.Format("Script {0} Error: {1}", CType(sender, ScriptTask).Name, e.Error.Message & vbCrLf & e.Error.StackTrace.ToString())
        Me.GameTracer.TraceMessage(msg)
    End Sub

    ''' <summary>
    ''' Handles OnCompilationFailed events of all the ScriptTask objects.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ScriptTask_OnCompilationFailed(ByVal sender As Object, ByVal e As CompilationErrorEventArgs)
        Dim msg = String.Format("Script {0} Error: {1}", CType(sender, CompilationErrorListener).Source.Name, e.Message)
        Me.GameTracer.TraceMessage(msg)
    End Sub

End Class